package store;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class EmployeeDAO {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;	

	public List<Employee> getAllEmployees() {
		
		return jdbcTemplate.query("select * from store.employee",
				new EmployeeRowMapper());		
	}
	
	private static final class EmployeeRowMapper implements RowMapper<Employee> {
		@Override
		public Employee mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {

			long id = resultSet.getLong("id");
			String name = resultSet.getString("name");

			Employee e = new Employee();
			
			e.setId(id);
			e.setName(name);
			
			return e;
		}

	}	

}
